import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ConfigService } from '@nestjs/config';
import { ValidationPipe, VersioningType } from '@nestjs/common';
import * as compression from 'compression';
import helmet from 'helmet';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { API } from './constants/response-check-path';

async function bootstrap() {
  const app = await NestFactory.create(AppModule, {
    logger: console,
    cors: true,
  });
  // 自动验证
  app.useGlobalPipes(new ValidationPipe({}));
  // 压缩
  app.use(compression());
  // 设置Helmet
  app.use(helmet());
  // 设置全局版本
  // app.setGlobalPrefix(API);
  // 设置url版本控制
  app.enableVersioning({
    type: VersioningType.URI,
    // defaultVersion: ['1'], // 因为根据若依改的，暂时先不要
  });
  // 设置swagger
  const config = new DocumentBuilder()
    .setTitle('Ruoyi 后端管理框架')
    .setDescription('使用ruoyi 前端vue3.0')
    .setVersion('1.0')
    .addBearerAuth()
    .build();
  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('swagger', app, document);
  // 端口
  const configService: ConfigService = app.get(ConfigService);
  const port = configService.get<string>('server.port');
  await app.listen(port);
  console.log({
    NODE_ENV: process.env.NODE_ENV,
    port,
    url: `http://localhost:${port}`,
  });
}
bootstrap();
